package com.ymttest.database.mongo.jproduct;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.mongodb.BasicDBObject;
import com.mongodb.WriteResult;
import com.ymt.database.MongoDBDriver;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.utils.MapUtil;

public class ProductsWapper {
	private MongoDBDriver mdd = YmtMongoDriverFactroy.getInstance_YmtProductDBdriverNewJava("Products");

	
	public Map<String,Object> getByProductId(String productId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	     
	public int getCountByProductId(String productId) throws Exception{
		return mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",50).size();
	}
	/**
	 * 根据直播Id获取商品
	 * @param spid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<String> getProductsBySpid(String spid) throws Exception {
		ArrayList<String> list = mdd.selectPart("{\"spid\":\"" + spid + "\"}");
		return list;
	}   
	/**
	 * 根据直播Id获取直播商品
	 * @param lid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductsBySpid(String spid,int productNum) throws Exception {
		Date date=new Date();
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + spid + "\"}","{\"ispsp\":1,\"spid\":1,\"pics\":1,\"minp\":1}",productNum,"{sort:1}");
		return list;
	}
	
	public boolean updateAll(Map<String, Object> searchMap,Map<String, Object> updateMap) throws Exception{
		BasicDBObject queryObject = new BasicDBObject(searchMap);
		BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
		WriteResult wr = mdd.getCollection().update(queryObject, updateObject,false,true);
		return  wr.isUpdateOfExisting();
	}
	
	
	public static void main(String[] args) throws Exception {
		
		System.out.println(new ProductsWapper().updateAll(MapUtil.hashMap("sid", 20336378, "isnew", true),MapUtil.hashMap("isnew", false)));
		//Mongo
		
	}
	    
}
